/*
 * date          Author     Note
 * 2015.04.03    wzwz       获取使用的度目、速度、罗拉段数
 * 2015.04.24    wzwz       辛克复位
 * 2015.06.29    wzwz       辛克恢复bug
 */




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mh/common.h>
#include <mh/sram.h>
#include <mh/mac.h>
#include "knit.h"
#include "prefixknit.h"
#include "backpiece.h"
#include "compress.h"



/*
 *
 */
int GetSourceYarnDrt(MH_KNIT *p, uchar yarn_used[16], ushort start_line)
{
    int ret = 0;
    memset(yarn_used, 0, 16);
    KNIT_PAGE *con = p->knit_page_head;
    int i = 0, j = 0;
    enum
    {
        LEFT = 1, RIGHT = 2
    };
    if(con && start_line <= p->total_line)
    {
        for(i = start_line; i <= p->total_line; ++i)
        {
            ushort gauze = 0;
            ushort gauze1 = con[i].gauze1_1;
            gauze1 <<= 8;
            gauze1 |= con[i].gauze1_9 & 0xFF;
            ushort gauze2 = con[i].gauze2_1;
            gauze2 <<= 8;
            gauze2 |= con[i].gauze2_9 & 0xFF;
            ushort gauze3 = con[i].gauze3_1;
            gauze3 <<= 8;
            gauze3 |= con[i].gauze3_9;
            ushort gauze4 = con[i].gauze4_1;
            gauze4 <<= 8;
            gauze4 |= con[i].gauze4_9;
            if(p->sys_info.cam == 1)
            {
                gauze = gauze1;
            }
            else
            {
                gauze = gauze1 | gauze2 | gauze3 | gauze4;
            }
            for(j = 0; j < 16; ++j)
            {
                if(!yarn_used[j])
                {
                    if((0x8000 >> j) & gauze)
                    {
                        yarn_used[j] = (i & 1) ? LEFT : RIGHT;
                        ++ret;
                    }
                }
            }
        }
        for(i = 1; i < start_line; ++i)
        {
            ushort gauze = 0;
            ushort gauze1 = con[i].gauze1_1;
            gauze1 <<= 8;
            gauze1 |= con[i].gauze1_9 & 0xFF;
            ushort gauze2 = con[i].gauze2_1;
            gauze2 <<= 8;
            gauze2 |= con[i].gauze2_9 & 0xFF;
            ushort gauze3 = con[i].gauze3_1;
            gauze3 <<= 8;
            gauze3 |= con[i].gauze3_9;
            ushort gauze4 = con[i].gauze4_1;
            gauze4 <<= 8;
            gauze4 |= con[i].gauze4_9;
            if(p->sys_info.cam == 1)
            {
                gauze = gauze1;
            }
            else
            {
                gauze = gauze1 | gauze2 | gauze3 | gauze4;
            }

            for(j = 0; j < 16; ++j)
            {
                if(!yarn_used[j])
                {
                    if((0x8000 >> j) & gauze)
                    {
                        yarn_used[j] = (i & 1) ? LEFT : RIGHT;
                        ++ret;
                    }
                }
            }
        }
    }
    return ret;
}
/*
 *
 */
void GetExchangedYarnDrt(MH_KNIT *p, uchar yarn_used[16])
{
    uchar data[16] = {0};
    int j = 0;
    for(j = 0; j < 16; ++j)
    {
        if(yarn_used[j])
        {
            if(p->gauze_exchange[0][j] && p->gauze_exchange[0][j] != j + 1)
            {
                if(!data[p->gauze_exchange[0][j] - 1])
                {
                    data[p->gauze_exchange[0][j] - 1] = yarn_used[j];
                }
            }
            else
            {
                if(!data[j])
                {
                    data[j] = yarn_used[j];
                }
            }
            if(p->gauze_exchange[1][j] && p->gauze_exchange[1][j] != j + 1)
            {
                if(!data[p->gauze_exchange[1][j] - 1])
                {
                    data[p->gauze_exchange[1][j] - 1] = yarn_used[j];
                }
            }
        }
    }
    memcpy(yarn_used, data, 16);
}
/*
 *
 */
ushort GetExchangedUsedYarn(MH_KNIT *p, ushort used)
{
    ushort ret = 0;
    int j = 0;
    for(j = 0; j < 16; ++j)
    {
        if(used & (0x8000 >> j))
        {
            if(p->gauze_exchange[0][j] && p->gauze_exchange[0][j] != j + 1)
            {
                ret |= 0x8000 >> (p->gauze_exchange[0][j] - 1);
            }
            else
            {
                ret |= 0x8000 >> j;
            }
            if(p->gauze_exchange[1][j] && p->gauze_exchange[1][j] != j + 1)
            {
                ret |= 0x8000 >> (p->gauze_exchange[1][j] - 1);
            }
        }
    }
    return ret;
}
/*
 *
 */
void ReplaceYarn(MH_KNIT *p, uchar src, uchar des)
{
    int h = 0;
    if(src == des)
    {
        return;
    }
    if(src >= 11 && src <= 18)
    {
        src -= 2;
    }
    if(des >= 11 && des <= 18)
    {
        des -= 2;
    }
    ushort src_bit = 0x8000 >> (src - 1);
    ushort des_bit = 0x8000 >> (des - 1);
    for(h = 0; h < p->total_line; ++h)
    {
        ushort bit = p->knit_page[h].gauze1_1;
        bit <<= 8;
        bit |= p->knit_page[h].gauze1_9;
        if(bit & src_bit)
        {
            bit ^= src_bit;
            bit |= des_bit;
        }
        p->knit_page[h].gauze1_1 = bit >> 8;
        p->knit_page[h].gauze1_9 = bit;

        bit = p->knit_page[h].gauze2_1;
        bit <<= 8;
        bit |= p->knit_page[h].gauze2_9;
        if(bit & src_bit)
        {
            bit ^= src_bit;
            bit |= des_bit;
        }
        p->knit_page[h].gauze2_1 = bit >> 8;
        p->knit_page[h].gauze2_9 = bit;

        bit = p->knit_page[h].gauze3_1;
        bit <<= 8;
        bit |= p->knit_page[h].gauze3_9;
        if(bit & src_bit)
        {
            bit ^= src_bit;
            bit |= des_bit;
        }
        p->knit_page[h].gauze3_1 = bit >> 8;
        p->knit_page[h].gauze3_9 = bit;

        bit = p->knit_page[h].gauze4_1;
        bit <<= 8;
        bit |= p->knit_page[h].gauze4_9;
        if(bit & src_bit)
        {
            bit ^= src_bit;
            bit |= des_bit;
        }
        p->knit_page[h].gauze4_1 = bit >> 8;
        p->knit_page[h].gauze4_9 = bit;

        uchar yinta = p->knit_page[h].yinta;
        if(yinta & 0x80)
        {
            uchar pos = yinta & 0x3F;
            if(src == 1)
            {
                switch(pos)
                {
                case 1:
                    p->knit_page[h].gauze_11 = des - 1;
                    break;
                case 2:
                    p->knit_page[h].gauze_12 = des - 1;
                    break;
                case 3:
                    p->knit_page[h].gauze_13 = des - 1;
                    break;
                case 4:
                    p->knit_page[h].gauze_14 = des - 1;
                    break;
                case 5:
                    p->knit_page[h].gauze_15 = des - 1;
                    break;
                case 6:
                    p->knit_page[h].gauze_16 = des - 1;
                    break;
                case 7:
                    p->knit_page[h].gauze_17 = des - 1;
                    break;
                case 8:
                    p->knit_page[h].gauze_18 = des - 1;
                    break;
                case 9:
                    p->knit_page[h].gauze_21 = des - 1;
                    break;
                case 10:
                    p->knit_page[h].gauze_22 = des - 1;
                    break;
                case 11:
                    p->knit_page[h].gauze_23 = des - 1;
                    break;
                case 12:
                    p->knit_page[h].gauze_24 = des - 1;
                    break;
                case 13:
                    p->knit_page[h].gauze_25 = des - 1;
                    break;
                case 14:
                    p->knit_page[h].gauze_26 = des - 1;
                    break;
                case 15:
                    p->knit_page[h].gauze_27 = des - 1;
                    break;
                case 16:
                    p->knit_page[h].gauze_28 = des - 1;
                    break;
                default:
                    printf("Warning: Invalid intasia: %s: %d\n", __FILE__, __LINE__);
                    break;
                }
                p->knit_page[h].yinta &= 0xC0;
            }
            else
            {
                pos = 0;
                if(p->knit_page[h].gauze_11 + 1 == src)
                {
                    p->knit_page[h].gauze_11 = des - 1;
                    pos = 1;
                }
                else if(p->knit_page[h].gauze_12 + 1 == src)
                {
                    p->knit_page[h].gauze_12 = des - 1;
                    pos = 2;
                }
                else if(p->knit_page[h].gauze_13 + 1 == src)
                {
                    p->knit_page[h].gauze_13 = des - 1;
                    pos = 3;
                }
                else if(p->knit_page[h].gauze_14 + 1 == src)
                {
                    p->knit_page[h].gauze_14 = des - 1;
                    pos = 4;
                }
                else if(p->knit_page[h].gauze_15 + 1 == src)
                {
                    p->knit_page[h].gauze_15 = des - 1;
                    pos = 5;
                }
                else if(p->knit_page[h].gauze_16 + 1 == src)
                {
                    p->knit_page[h].gauze_16 = des - 1;
                    pos = 6;
                }
                else if(p->knit_page[h].gauze_17 + 1 == src)
                {
                    p->knit_page[h].gauze_17 = des - 1;
                    pos = 7;
                }
                else if(p->knit_page[h].gauze_18 + 1 == src)
                {
                    p->knit_page[h].gauze_18 = des - 1;
                    pos = 8;
                }
                else if(p->knit_page[h].gauze_21 + 1 == src)
                {
                    p->knit_page[h].gauze_21 = des - 1;
                    pos = 9;
                }
                else if(p->knit_page[h].gauze_22 + 1 == src)
                {
                    p->knit_page[h].gauze_22 = des - 1;
                    pos = 10;
                }
                else if(p->knit_page[h].gauze_23 + 1 == src)
                {
                    p->knit_page[h].gauze_23 = des - 1;
                    pos = 11;
                }
                else if(p->knit_page[h].gauze_24 + 1 == src)
                {
                    p->knit_page[h].gauze_24 = des - 1;
                    pos = 12;
                }
                else if(p->knit_page[h].gauze_25 + 1 == src)
                {
                    p->knit_page[h].gauze_25 = des - 1;
                    pos = 13;
                }
                else if(p->knit_page[h].gauze_26 + 1 == src)
                {
                    p->knit_page[h].gauze_26 = des - 1;
                    pos = 14;
                }
                else if(p->knit_page[h].gauze_27 + 1 == src)
                {
                    p->knit_page[h].gauze_27 = des - 1;
                    pos = 15;
                }
                else if(p->knit_page[h].gauze_28 + 1 == src)
                {
                    p->knit_page[h].gauze_28 = des - 1;
                    pos = 16;
                }
                //pos of yarn1
                if(des == 1)
                {
                    p->knit_page[h].yinta &= 0xC0;
                    p->knit_page[h].yinta |= pos;
                }
            }
        }
    }
}
/*
 *
 */
uint GetUsedDensitySect(MH_KNIT *p)
{
    uint ret = 0;
    int h = 0;
    for(h = 0; h < p->total_line; ++h)
    {
        uchar dumu = p->knit_page[h].dumu1;
        if(dumu)
        {
            ret |= 1 << (dumu - 1);
        }
        dumu = p->knit_page[h].dumu2;
        if(dumu)
        {
            ret |= 1 << (dumu - 1);
        }
    }
    return ret;
}
/*
 *
 */
uint GetUsedSpeedSect(MH_KNIT *p)
{
    uint ret = 0;
    int h = 0;
    for(h = 0; h < p->total_line; ++h)
    {
        uchar speed = p->knit_page[h].speed;
        if(speed)
        {
            ret |= 1 << (speed - 1);
        }
    }
    return ret;
}
/*
 * high roll/aux rab
 */
uint GetUsedRabSect(MH_KNIT *p)
{
    uint ret = 0;
    int h = 0;
    for(h = 0; h < p->total_line; ++h)
    {
        uchar rab = p->knit_page[h].high_roll;
        if(rab)
        {
            ret |= 1 << (rab - 1);
        }
    }
    return ret;
}
/*
 * low roll / master rab
 */
uint GetUsedRollSect(MH_KNIT *p)
{
    uint ret = 0;
    int h = 0;
    for(h = 0; h < p->total_line; ++h)
    {
        uchar roll = p->knit_page[h].low_roll;
        if(roll)
        {
            ret |= 1 << (roll - 1);
        }
    }
    return ret;
}
/*
 * low roll / master rab
 */
uint GetUsedSwitchSect(MH_KNIT *p)
{
    uint ret = 0;
    int h = 0;
    for(h = 0; h < p->total_line; ++h)
    {
        uchar kaihe = p->knit_page[h].kaihe;
        if(kaihe)
        {
            ret |= 1 << (kaihe - 1);
        }
    }
    return ret;
}
/*
 *
 */
int ResetKnitSink(MH_KNIT *p, int f0b1)
{
    int ret = 0;
    if(p->machine_ok)
    {
        if(p->communication == COMM_SRAM)
        {
            short value = f0b1 ? p->back_sink_reset_value : p->front_sink_reset_value;
            ret = SramResetKnitSink(p->pmac, f0b1, value);
        }
    }
    return ret;
}
/*
 *
 */
uchar GetRunYarnOut(MH_KNIT *p, int l0r1, int which)
{
    uchar ret = 0;
    l0r1 = l0r1 ? 1 : 0;
    if(which < 16)
    {
        ret = p->yarn_out.stop[l0r1][which];
    }
    return ret;
}
/*
 *
 */
void SetRunYarnOut(MH_KNIT *p, int l0r1, int which, uchar stop)
{
    l0r1 = l0r1 ? 1 : 0;
    if(which < 16)
    {
        p->yarn_out.stop[l0r1][which] = stop;
    }
}
/*
 *
 */
int RestoreDumuAndSink(MH_KNIT *p, PHEAD_TEST phead_test)
{
    int ret = 0;
    uchar data[64] = {0};
    short *des = (short *)&data[2];
    uchar *valid = &data[22];
    int need = FALSE;
    valid[0] = phead_test->dumu_state[0][0] != p->dumu_state[0][0];
    valid[1] = phead_test->dumu_state[0][1] != p->dumu_state[0][1];
    valid[2] = phead_test->dumu_state[1][0] != p->dumu_state[1][0];
    valid[3] = phead_test->dumu_state[1][1] != p->dumu_state[1][1];
    valid[4] = phead_test->sink_state[0][0] != p->sink_state[0][0];
    valid[5] = phead_test->dumu_state[0][2] != p->dumu_state[0][2];
    valid[6] = phead_test->dumu_state[0][3] != p->dumu_state[0][3];
    valid[7] = phead_test->dumu_state[1][2] != p->dumu_state[1][2];
    valid[8] = phead_test->dumu_state[1][3] != p->dumu_state[1][3];
    valid[9] = phead_test->sink_state[1][0] != p->sink_state[1][0];
    if(valid[0])
    {
        des[0] = phead_test->dumu_state[0][0];
        need = TRUE;
    }
    if(valid[1])
    {
        des[1] = phead_test->dumu_state[0][1];
        need = TRUE;
    }
    if(valid[2])
    {
        des[2] = phead_test->dumu_state[1][0];
        need = TRUE;
    }
    if(valid[3])
    {
        des[3] = phead_test->dumu_state[1][1];
        need = TRUE;
    }
    if(valid[4])
    {
        des[4] = phead_test->sink_state[0][0];
        need = TRUE;
    }
    if(valid[5])
    {
        des[5] = phead_test->dumu_state[0][2];
        need = TRUE;
    }
    if(valid[6])
    {
        des[6] = phead_test->dumu_state[0][3];
        need = TRUE;
    }
    if(valid[7])
    {
        des[7] = phead_test->dumu_state[1][2];
        need = TRUE;
    }
    if(valid[8])
    {
        des[8] = phead_test->dumu_state[1][3];
        need = TRUE;
    }
    if(valid[9])
    {
        des[9] = phead_test->sink_state[1][0];
        need = TRUE;
    }
    if(need)
    {
        if(p->communication == COMM_SRAM)
        {
            ret = SramRestoreDumuAndSink(p->pmac, data, sizeof(data));
        }
    }
    else
    {
        ret = TRUE;
    }

    return ret;
}
/*
 *
 */
uchar GetGauzeType(MH_KNIT *p)
{
    return p->gauze_type;
}
/*
 *
 */
void SetGauzeType(MH_KNIT *p, uchar type)
{
    p->gauze_type = type;
}
